package com.lyn.hy;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Model;
import com.lyn.common.base.BaseService;
import com.lyn.common.base.Result;
import com.lyn.config.Man;
import com.lyn.domain.TblDataHyydflb;
import com.lyn.mango.MangoDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

public class HyydflbService {
    Logger logger = LoggerFactory.getLogger(HyydflbService.class);
    public static final HyydflbService me = new HyydflbService();

    //插入单条记录
    public void insert(String data) {
        logger.info("mango:插入一条记录");
        TblDataHyydflb model = JSONObject.parseObject(data, TblDataHyydflb.class);
        HyydflbDao dao = Man.get().create(HyydflbDao.class);
        dao.insert(model);
    }

    //根据主键更新单条记录
    public void updateById(String data) {
        logger.info("mango:根据主键更新单条记录");
        TblDataHyydflb model = JSONObject.parseObject(data, TblDataHyydflb.class);
        HyydflbDao dao = Man.get().create(HyydflbDao.class);
        dao.updateById(model);
    }

    //根据主键更新多条记录
    public void updatesById(String dataArray) {
        logger.info("mango:根据主键更新多条记录");
        JSONArray ja = JSONArray.parseArray(dataArray);
        for(int i=0; i<ja.size(); i++) {
            TblDataHyydflb model = ja.getObject(i, TblDataHyydflb.class);
            HyydflbDao dao = Man.get().create(HyydflbDao.class);
            dao.updateById(model);
        }
    }

    //自定义条件更新若干条记录
    public void customUpdate(String data) {
        logger.info("mango:自定义条件更新若干条记录");
        TblDataHyydflb model = JSONObject.parseObject(data, TblDataHyydflb.class);
        HyydflbDao dao = Man.get().create(HyydflbDao.class);
        dao.customUpdate(model);
    }

    //根据主键获取单条记录
    public Result getById(String id, int nian) {
        logger.info("mango:根据主键获取单条记录");
        TblDataHyydflb model = new TblDataHyydflb();
        model.setGuid(id);
        model.setNian(new BigDecimal(nian));
        HyydflbDao dao = Man.get().create(HyydflbDao.class);
        TblDataHyydflb result = dao.getById(model);
        return Result.ok("查询成功", result);
    }

    //自定义条件查询若干条记录
    public Result customSelect(int nian, int yue, String dwdm) {
        logger.info("mango:自定义条件查询若干条记录");
        TblDataHyydflb model = new TblDataHyydflb();
        model.setNian(new BigDecimal(nian));
        model.setYue(new BigDecimal(yue));
        model.setDwdm(dwdm);
        HyydflbDao dao = Man.get().create(HyydflbDao.class);
        List<TblDataHyydflb> list = dao.customSelect(model);
        return Result.ok("查询成功", list);
    }

    //in语句查询
    public Result customSelectIn(int nian, int yue, String dwdm, String tjm) {
        logger.info("mango:in语句查询");
        HyydflbDao dao = Man.get().create(HyydflbDao.class);
        List<String> tjms = Arrays.asList(tjm.split(","));
        List<TblDataHyydflb> list = dao.customSelectIn(new BigDecimal(nian), new BigDecimal(yue), dwdm, tjms);
        return Result.ok("查询成功", list);
    }

    //like语句查询
    public Result customSelectLike(int nian, int yue, String dwdm, String tjm) {
        logger.info("mango:like语句查询");
        HyydflbDao dao = Man.get().create(HyydflbDao.class);
        List<TblDataHyydflb> list = dao.customSelectLike(new BigDecimal(nian), new BigDecimal(yue), dwdm, tjm+"%");
        return Result.ok("查询成功", list);
    }

    //join语句查询
    public Result customSelectJoin(int nian, int yue, String dwdm) {
        logger.info("mango:join语句查询");
        HyydflbDao dao = Man.get().create(HyydflbDao.class);
        List<HyVo> list = dao.customSelectJoin(new BigDecimal(nian), new BigDecimal(yue), dwdm);
        return Result.ok("查询成功", list);
    }
}
